package com.ruoyi.ncpcs.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.ncpcs.domain.BaseReq;
import com.ruoyi.ncpcs.domain.DataQueryReq;
import com.ruoyi.ncpcs.service.INcpcsInterfaceFieldsService;
import com.ruoyi.ncpcs.utils.InterfaceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.ncpcs.domain.NcpcsInterfaceInfo;
import com.ruoyi.ncpcs.service.INcpcsInterfaceInfoService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * interfaceManageController
 *
 * @author ruoyi
 * @date 2022-09-25
 */
@RestController
@RequestMapping("/ncpcs/upload/interfaceInfo")
public class NcpcsInterfaceInfoController extends BaseController {
    @Autowired
    private INcpcsInterfaceInfoService ncpcsInterfaceInfoService;

    @Autowired
    private INcpcsInterfaceFieldsService ncpcsInterfaceFieldsService;

    /**
     * 查询interfaceManage列表
     */
    @RequiresPermissions("@ss.hasPermi('upload:interfaceInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(NcpcsInterfaceInfo ncpcsInterfaceInfo) {
        startPage();
        List<NcpcsInterfaceInfo> list = ncpcsInterfaceInfoService.selectNcpcsInterfaceInfoList(ncpcsInterfaceInfo);
        return getDataTable(list);
    }

    /**
     * 导出interfaceManage列表
     */
    @RequiresPermissions("@ss.hasPermi('upload:interfaceInfo:export')")
    @Log(title = "interfaceManage", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, NcpcsInterfaceInfo ncpcsInterfaceInfo) {
        List<NcpcsInterfaceInfo> list = ncpcsInterfaceInfoService.selectNcpcsInterfaceInfoList(ncpcsInterfaceInfo);
        ExcelUtil<NcpcsInterfaceInfo> util = new ExcelUtil<>(NcpcsInterfaceInfo.class);
        util.exportExcel(response, list, "interfaceManage数据");
    }

    /**
     * 获取interfaceManage详细信息
     */
    @RequiresPermissions("@ss.hasPermi('upload:interfaceInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(ncpcsInterfaceInfoService.selectNcpcsInterfaceInfoById(id));
    }

    /**
     * 新增interfaceManage
     */
    @RequiresPermissions("@ss.hasPermi('upload:interfaceInfo:add')")
    @Log(title = "interfaceManage", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NcpcsInterfaceInfo ncpcsInterfaceInfo) {
        return toAjax(ncpcsInterfaceInfoService.insertNcpcsInterfaceInfo(ncpcsInterfaceInfo));
    }

    /**
     * 修改interfaceManage
     */
    @RequiresPermissions("@ss.hasPermi('upload:interfaceInfo:edit')")
    @Log(title = "interfaceManage", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NcpcsInterfaceInfo ncpcsInterfaceInfo) {
        return toAjax(ncpcsInterfaceInfoService.updateNcpcsInterfaceInfo(ncpcsInterfaceInfo));
    }

    /**
     * 删除interfaceManage
     */
    @RequiresPermissions("@ss.hasPermi('upload:interfaceInfo:remove')")
    @Log(title = "interfaceManage", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ncpcsInterfaceInfoService.deleteNcpcsInterfaceInfoByIds(ids));
    }

    /**
     * 上传接口信息
     */
    @Log(title = "interfaceManage", businessType = BusinessType.UPLOAD)
    @PostMapping("/uploadData")
    public AjaxResult uploadData(@RequestBody DataQueryReq queryReq) {
        BaseReq req = new BaseReq();
        // 1.组合上传请求参数
        InterfaceUtils.buildInterfaceReq(queryReq, req);
        // 2.组合上传业务数据
        // 2.1 拼接查询业务数据查询字段
        queryReq.setBaseQuerySql(ncpcsInterfaceFieldsService.getBaseQuerySql(queryReq.getInterfaceId()));
        // 获取查询表名
        queryReq.setQueryTable(ncpcsInterfaceFieldsService.getQueryTable(queryReq.getInterfaceId()));
        // 获取主键名称
        queryReq.setPkColumn(ncpcsInterfaceFieldsService.getPkColumn(queryReq.getInterfaceId()));
        // 2.2 查询业务数据并转成JSON对象
        List<HashMap<String, Object>> uploadDataList = ncpcsInterfaceFieldsService.getData(queryReq);
        JSONObject obj = new JSONObject();
        obj.put("dataList", uploadDataList);
        // 2.3 转成NcData字符串格式
        req.setNcData(obj.toJSONString());
        // 3.使用RestTemplate发起请求与接收返回值
        Map<String, Set> resultData = InterfaceUtils.uploadData(req);
        ncpcsInterfaceFieldsService.dealUploadSuccess(resultData.get("uploadSuccess"), queryReq.getQueryTable());
        ncpcsInterfaceFieldsService.dealUploadFail(resultData.get("uploadFail"), queryReq.getQueryTable());
        return AjaxResult.success(resultData.get("resultData"));
    }

}
